package org.jeecg.modules.job.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.job.wms.entity.WmsResume;
import org.jeecg.modules.job.wms.entity.WmsProExp;
import org.jeecg.modules.job.wms.entity.WmsWorkExp;
import org.jeecg.modules.job.wms.entity.WmsEduExp;
import org.jeecg.modules.job.wms.entity.WmsJobExpect;
import org.jeecg.modules.job.wms.entity.WmsResumeFile;
import org.jeecg.modules.job.wms.mapper.WmsProExpMapper;
import org.jeecg.modules.job.wms.mapper.WmsWorkExpMapper;
import org.jeecg.modules.job.wms.mapper.WmsEduExpMapper;
import org.jeecg.modules.job.wms.mapper.WmsJobExpectMapper;
import org.jeecg.modules.job.wms.mapper.WmsResumeFileMapper;
import org.jeecg.modules.job.wms.mapper.WmsResumeMapper;
import org.jeecg.modules.job.wms.service.IWmsResumeService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 个人简历
 * @Author: jeecg-boot
 * @Date:   2022-12-18
 * @Version: V1.0
 */
@Service
public class WmsResumeServiceImpl extends ServiceImpl<WmsResumeMapper, WmsResume> implements IWmsResumeService {

	@Resource
	private WmsResumeMapper wmsResumeMapper;
	@Resource
	private WmsProExpMapper wmsProExpMapper;
	@Resource
	private WmsWorkExpMapper wmsWorkExpMapper;
	@Resource
	private WmsEduExpMapper wmsEduExpMapper;
	@Resource
	private WmsJobExpectMapper wmsJobExpectMapper;
	@Resource
	private WmsResumeFileMapper wmsResumeFileMapper;
	@Resource
	private ISysBaseAPI sysBaseAPI;

	@Override
	public IPage<WmsResume> getResumePageList(Page<WmsResume> page, WmsResume params) {
		IPage<WmsResume> pageList=wmsResumeMapper.getResumePageList(page,params);
		return pageList;
	}

	@Override
	public WmsResume getResume(WmsResume param) {
		WmsResume resume=this.getOne(new QueryWrapper<>(param));
		List<WmsWorkExp> workExpResult=wmsWorkExpMapper.selectByMainId(resume.getId());
		resume.setWorkExpList(workExpResult);
		List<WmsProExp> proExpResults=wmsProExpMapper.selectByMainId(resume.getId());
		resume.setProExpList(proExpResults);
		List<WmsEduExp> eduExpResults=wmsEduExpMapper.selectByMainId(resume.getId());
		resume.setEduExpList(eduExpResults);
		List<WmsJobExpect> jobExpectResults=wmsJobExpectMapper.selectByMainId(resume.getId());
		resume.setJobExpectList(jobExpectResults);
		return resume;
	}

	@Override
	public boolean updateResumeInfo(WmsResume param) {
		//判断ID是否存在，如果不存在，则新建
		WmsResume resumeResult=getOne(new QueryWrapper<WmsResume>().eq("member_id",param.getMemberId()));
		if (resumeResult!=null){
			param.setId(resumeResult.getId());
			this.updateById(param);
		}else{
			this.save(param);
		}
		//更新实名信息
		LoginUser memberParam=new LoginUser();
		memberParam.setRealname(param.getName());
		memberParam.setId(param.getMemberId());
		memberParam.setAvatar(param.getAvatar());
		sysBaseAPI.updateUserInfo(memberParam);
		return false;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(WmsResume wmsResume, List<WmsProExp> wmsProExpList,List<WmsWorkExp> wmsWorkExpList,List<WmsEduExp> wmsEduExpList,List<WmsJobExpect> wmsJobExpectList,List<WmsResumeFile> wmsResumeFileList) {
		wmsResumeMapper.insert(wmsResume);
		if(wmsProExpList!=null && wmsProExpList.size()>0) {
			for(WmsProExp entity:wmsProExpList) {
				//外键设置
				entity.setResumeId(wmsResume.getId());
				wmsProExpMapper.insert(entity);
			}
		}
		if(wmsWorkExpList!=null && wmsWorkExpList.size()>0) {
			for(WmsWorkExp entity:wmsWorkExpList) {
				//外键设置
				entity.setResumeId(wmsResume.getId());
				wmsWorkExpMapper.insert(entity);
			}
		}
		if(wmsEduExpList!=null && wmsEduExpList.size()>0) {
			for(WmsEduExp entity:wmsEduExpList) {
				//外键设置
				entity.setResumeId(wmsResume.getId());
				wmsEduExpMapper.insert(entity);
			}
		}
		if(wmsJobExpectList!=null && wmsJobExpectList.size()>0) {
			for(WmsJobExpect entity:wmsJobExpectList) {
				//外键设置
				entity.setResumeId(wmsResume.getId());
				wmsJobExpectMapper.insert(entity);
			}
		}
		if(wmsResumeFileList!=null && wmsResumeFileList.size()>0) {
			for(WmsResumeFile entity:wmsResumeFileList) {
				//外键设置
				entity.setResumeId(wmsResume.getId());
				wmsResumeFileMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(WmsResume wmsResume,List<WmsProExp> wmsProExpList,List<WmsWorkExp> wmsWorkExpList,List<WmsEduExp> wmsEduExpList,List<WmsJobExpect> wmsJobExpectList,List<WmsResumeFile> wmsResumeFileList) {
		wmsResumeMapper.updateById(wmsResume);
		
		//1.先删除子表数据
		wmsProExpMapper.deleteByMainId(wmsResume.getId());
		wmsWorkExpMapper.deleteByMainId(wmsResume.getId());
		wmsEduExpMapper.deleteByMainId(wmsResume.getId());
		wmsJobExpectMapper.deleteByMainId(wmsResume.getId());
		wmsResumeFileMapper.deleteByMainId(wmsResume.getId());
		
		//2.子表数据重新插入
		if(wmsProExpList!=null && wmsProExpList.size()>0) {
			for(WmsProExp entity:wmsProExpList) {
				//外键设置
				entity.setResumeId(wmsResume.getId());
				wmsProExpMapper.insert(entity);
			}
		}
		if(wmsWorkExpList!=null && wmsWorkExpList.size()>0) {
			for(WmsWorkExp entity:wmsWorkExpList) {
				//外键设置
				entity.setResumeId(wmsResume.getId());
				wmsWorkExpMapper.insert(entity);
			}
		}
		if(wmsEduExpList!=null && wmsEduExpList.size()>0) {
			for(WmsEduExp entity:wmsEduExpList) {
				//外键设置
				entity.setResumeId(wmsResume.getId());
				wmsEduExpMapper.insert(entity);
			}
		}
		if(wmsJobExpectList!=null && wmsJobExpectList.size()>0) {
			for(WmsJobExpect entity:wmsJobExpectList) {
				//外键设置
				entity.setResumeId(wmsResume.getId());
				wmsJobExpectMapper.insert(entity);
			}
		}
		if(wmsResumeFileList!=null && wmsResumeFileList.size()>0) {
			for(WmsResumeFile entity:wmsResumeFileList) {
				//外键设置
				entity.setResumeId(wmsResume.getId());
				wmsResumeFileMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		wmsProExpMapper.deleteByMainId(id);
		wmsWorkExpMapper.deleteByMainId(id);
		wmsEduExpMapper.deleteByMainId(id);
		wmsJobExpectMapper.deleteByMainId(id);
		wmsResumeFileMapper.deleteByMainId(id);
		wmsResumeMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			wmsProExpMapper.deleteByMainId(id.toString());
			wmsWorkExpMapper.deleteByMainId(id.toString());
			wmsEduExpMapper.deleteByMainId(id.toString());
			wmsJobExpectMapper.deleteByMainId(id.toString());
			wmsResumeFileMapper.deleteByMainId(id.toString());
			wmsResumeMapper.deleteById(id);
		}
	}
	
}
